import { Component, OnInit } from '@angular/core';
import { ApplyFundGroupService } from './apply-fund-group.service';
import * as _ from "lodash";
import { DataService } from '../../../../../services/data.service';
@Component({
  selector: 'app-apply-fund-group',
  templateUrl: './apply-fund-group.component.html',
  styleUrls: ['./apply-fund-group.component.scss'],
  providers: [ApplyFundGroupService]
})
export class ApplyFundGroupComponent implements OnInit {
  //发送过来的数据下标
  public index: any;
  //从上一层获取到的默认勾选的数据id
  public checkedIdList: any = [];
  //判断查询按钮是否已点击
  public searchIsChecked: any = false;
  //模式类型
  public modeType: any;//input表示录入模式，route表示在途模式
  //根据类型分类的list
  public fundTypeList: any;
  public fundTypeInfo: any;
  //根据搜索条件得到的list
  public searchList: any = [];
  //基金组list
  public groupList: any = [];
  //账户名称
  public sFundName: any;
  //产品分类以及搜索全选按钮
  public typeCheckedAll: any = false;
  //基金组全选按钮
  public groupCheckedAll: any = false;
  //勾选的数据
  public checkList: any = [];
  //
  public checkNoList: any = [];
  //当前tab类型
  public tabType: any = [];
  //公募QDIItab分页配置
  public listPageInfo = {
    currentPageNum: 1,
    pageSize: 50,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  public getBody = {
    pageIndex: 1,
    pageSize: 50,
    sFundType: 1,
    sType: 0  //0代表非QDII，1代表QDII
  }
  //保存时已选择的用户和基金组
  constructor(
    public applyFundGroupService: ApplyFundGroupService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    //将接受到的数据转为number类型
    _.forEach(this.checkedIdList, (item, index) => {
      this.checkedIdList[index] = parseInt(this.checkedIdList[index]);
    })
    this.checkNoList = _.cloneDeep(this.checkedIdList);
    this.listByFundType(1);
    this.fundGroupList();
  }
  // 条数分页
  sizeChange(pageSize: number, type) {
    let that = this;
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      if (type == 'tab') {
        that.listByFundType(that.tabType);
      } else if (type == 'search') {
        that.searchlist();
      }

    }
  }
  // 列表分页
  pageNavigation(currentPageNum: number, type) {
    let that = this;
    this.listPageInfo.currentPageNum = currentPageNum;
    if (type == 'tab') {
      this.listByFundType(this.tabType, this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
    } else if (type == 'search') {
      this.searchlist(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize)
    }
  }
  // 列表全选
  checkedAll(data, checkedAll) {

    if (checkedAll) {
      // 更新为全选

      _.forEach(data, item => {
        item.isChecked = true;
        this.checkList.push(item);
        this.checkNoList.push(item.nFundNo);
      });
    } else {
      // 更新为不全选
      _.forEach(data, item => {
        item.isChecked = false;
        this.checkList = _.filter(this.checkList, (it: any) => {
          return it.nFundNo != item.nFundNo;
        })
        this.checkNoList = _.filter(this.checkNoList, (it: any) => {
          return it != item.nFundNo;
        })
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll(data, checkAllBtn) {
    let that = this;
    let checkList = [];
    _.forEach(data, item => {
      if (item.isChecked == true) {
        checkList.push(item);
        this.checkList.push(item);
        this.checkNoList.push(item.nFundNo);
      } else {
        this.checkList = _.filter(this.checkList, (it: any) => {
          return it.nFundNo != item.nFundNo;
        })
        this.checkNoList = _.filter(this.checkNoList, (it: any) => {
          return it != item.nFundNo;
        })
      }
    })
    this.checkList = _.uniq(this.checkList);
    if (checkList.length == data.length) {
      this.checkedAllType(checkAllBtn, true);
    } else {
      this.checkedAllType(checkAllBtn, false);
    }
  }
  //判断全选按钮类型
  checkedAllType(checkAllBtn, isTrue) {
    switch (checkAllBtn) {
      //产品类型全选按钮
      case 'typeCheckedAll':
        this.typeCheckedAll = isTrue;
        break;
      //基金组全选按钮
      case 'groupCheckedAll':
        this.groupCheckedAll = isTrue;
        break;
    }
  }
  //tab切换
  tabChange(index){
     this.listPageInfo.pageSize = 50;
     this.listByFundType(index);
  }
  //适用名称搜索弹出框产品类型tab
  listByFundType(index, pageNum?, pageSize?) {
    let that = this;
    this.getBody.sFundType = index;
    this.tabType = index;
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.applyFundGroupService.listByFundType(this.getBody).subscribe(data => {
      if (data) {
        _.forIn(data, (value, key) => {
          this.fundTypeList = value.list;
          this.listPageInfo.currentPageNum = value.pageNum;
          this.listPageInfo.totalPages = value.pages;
          this.listPageInfo.total = value.total;
          this.listPageInfo.startRow = value.startRow;
          this.listPageInfo.endRow = value.endRow;

          _.forEach(this.fundTypeList, item => {
            if (_.includes(this.checkNoList, item.nFundNo)) {
              item.isChecked = true;
            }
          })
        });
        this.ctrlCheckAll(this.fundTypeList, 'typeCheckedAll')
        // if (this.checkedIdList.length > 0) {
        //   _.forIn(that.fundTypeInfo, (value, key) => {
        //     _.forEach(value, item => {
        //       if (_.includes(that.checkedIdList, item.nFundNo)) {
        //         item.isChecked = true;
        //       }
        //     })
        //   })
        // }
        // console.log(data);
        // this.selectType('公募基金');
      }
    })
  }
  //点击tab根据产品类型将数据添加到list中
  // selectType(type) {
  //   let that = this;
  //   _.forIn(this.fundTypeInfo, (value, key) => {
  //     if (type == key) {
  //       that.fundTypeList = value;
  //       that.ctrlCheckAll(that.fundTypeList, 'typeCheckedAll');
  //     }
  //   })
  // }
  //获取基金组信息列表
  fundGroupList() {
    let that = this;
    let getBody = {
      sType: 0
    }
    this.applyFundGroupService.fundGroupList(getBody).subscribe(data => {
      if (data) {
        this.groupList = data;
        if (this.checkedIdList.length > 0) {
          _.forEach(this.groupList, item => {
            if (_.includes(that.checkedIdList, item.nFgId)) {
              item.isChecked = true;
            }
          })
        }
      }
    })
  }
  //适用名称搜索弹出框搜索
  searchlist(pageNum?, pageSize?) {
    let that = this;
    this.searchIsChecked = true;
    this.getBody.sFundType = this.tabType;
    this.searchTabCheck();
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    let getBody = _.cloneDeep(this.getBody);
    getBody['sFundName'] = this.sFundName;
    this.applyFundGroupService.searchList(getBody).subscribe(data => {
      if (data) {

        this.searchList = data.list;
        this.listPageInfo.currentPageNum = data.pageNum;
        this.listPageInfo.totalPages = data.pages;
        this.listPageInfo.total = data.total;
        this.listPageInfo.startRow = data.startRow;
        this.listPageInfo.endRow = data.endRow;
        // that.ctrlCheckAll(this.searchList, 'typeCheckedAll');
      }
    })
  }
  //保存选择的用户
  saveSelect() {
    let that = this;
    let saveList: any = [];
    let saveListItem: any;
    let sFundNoList: any = [];
    let sFundNoItem: any;

    _.forEach(this.checkList, item => {
      saveList.push(item.sFundName);
      sFundNoList.push(item.nFundNo);
    })
    _.forEach(this.searchList, item => {
      if (item.isChecked) {
        saveList.push(item.sFundName);
        sFundNoList.push(item.nFundNo);
      }
    })
    _.forEach(this.groupList, item => {
      if (item.isChecked) {
        saveList.push(item.sFgName);
        sFundNoList.push(item.nFgId);
      }
    })
    saveList = _.uniq(saveList);
    saveListItem = saveList.join(',');
    that.dataService.nQDIIFGroupInfo = saveListItem;
    sFundNoList = _.uniq(sFundNoList);
    sFundNoItem = sFundNoList.join(',');
    that.dataService.nQDIIIdList = sFundNoItem;
    if (this.modeType == 'input') {
      that.dataService.nQDIIInputModelEmitter.emit('inputModel');
    } else if (this.modeType == 'route') {
      that.dataService.nQDIIInputModelEmitter.emit('routeModel');
    } else if (this.index != null) {
      that.dataService.nQDIIIndexId = this.index;
      console.log(that.dataService.nQDIIIndexId)
      that.dataService.nQDIIInputModelEmitter.emit('index');
    }

    console.log(saveListItem)
    console.log(sFundNoItem)
  }

  //关闭modal
  hideModal() {
    window['$']('#applyFundGroup').modal('hide');
    window['$']('#applyFundGroup1').modal('hide');
    window['$']('#viewApplyFundGroup').modal('hide');
  }
  //点击搜索按钮tab跳转到搜索结果
  searchTabCheck(){
    window['$']("#fundType1").removeClass('active');
    window['$']("#tab_fundType1").attr('aria-expanded', "false");
    window['$']("#fundType2").removeClass('active');
    window['$']("#tab_fundType2").attr('aria-expanded', "false");
    window['$']("#fundType3").removeClass('active');
    window['$']("#tab_fundType3").attr('aria-expanded', "false");    
    window['$']("#fundType4").removeClass('active');
    window['$']("#tab_fundType4").attr('aria-expanded', "false");
    window['$']("#fundType5").removeClass('active');
    window['$']("#tab_fundType5").attr('aria-expanded', "false");
    window['$']("#fundType6").removeClass('active');
    window['$']("#tab_fundType6").attr('aria-expanded', "false");
    window['$']("#fundType7").removeClass('active');
    window['$']("#tab_fundType7").attr('aria-expanded', "false");
    window['$']("#tab_fundType").removeClass('active');

    window['$']("#fundGroup").removeClass('active');
    window['$']("#tab_fundGroup").removeClass('active');
    window['$']("#tab_fundGroup1").attr('aria-expanded', "false");    

    window['$']("#search").addClass('active');
    window['$']("#tab_search").addClass('active');
    window['$']("#tab_search1").attr('aria-expanded', "true");
  }
}
